﻿using System.Linq.Expressions;

namespace Quartz.MUI.ClassLibrary.Model
{
    public static class ExpressionHelper
    {

        /// <summary>
        /// 创建一个表达式
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="expression"></param>
        /// <returns></returns>
        public static Expression<Func<T, bool>> Create<T>(Expression<Func<T, bool>> expression)
        {
            return expression;
        }

        /// <summary>
        /// 创建一个表达式
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="expression"></param>
        /// <param name="cond"></param>
        /// <returns></returns>
        public static Expression<Func<T, bool>> Create<T>(Expression<Func<T, bool>> expression, bool cond)
        {
            return cond ? expression : x => true;
        }

        /// <summary>
        /// 创建一个空表达式
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static Expression<Func<T, bool>> Create<T>()
        {
            return x => true;
        }

        public static Expression<Func<T, bool>> CreateAnd<T>(params (Expression<Func<T, bool>> expression, bool cond)[] ps)
        {
            var expressions = ps.Where(x => x.cond).Select(x => x.expression);
            return expressions.Any() ? expressions.MergeAnd() : x => true;
        }

        public static Expression<Func<T, bool>> CreateOr<T>(params (Expression<Func<T, bool>> expression, bool cond)[] ps)
        {
            var expressions = ps.Where(x => x.cond).Select(x => x.expression);
            return expressions.Any() ? expressions.MergeOr() : x => true;
        }
    }
}
